$(function () {
    $("#loadbtn").click(function () {

        $.ajax({
            url: "/load_data",
            method: "get",
            data: {
                "city": "郑州"
            },
            success: function (res) {
                var dataDiv = document.getElementById('data-display');

                dataDiv.innerHTML = '';

                // 显示状态信息
                var statusDiv = document.createElement('div');
                statusDiv.innerHTML = '<strong>状态码:</strong> ' + res.code +
                    ' | <strong>消息:</strong> ' + res.msg;
                dataDiv.appendChild(statusDiv);

                if (res.data && res.data.length > 0 && res.data[0].dateValueMap) {
                    var cityData = res.data[0];

                    // 创建表格
                    var table = document.createElement('table');
                    table.className = 'steel-data-table';

                    // 创建表头
                    var thead = document.createElement('thead');
                    thead.innerHTML = '<tr><th>日期</th><th>价格 (元/吨)</th></tr>';
                    table.appendChild(thead);

                    // 创建表格内容
                    var tbody = document.createElement('tbody');
                    cityData.dateValueMap.forEach(function (item) {
                        var row = document.createElement('tr');
                        row.innerHTML = '<td>' + item.date + '</td><td>' + item.value + '</td>';
                        tbody.appendChild(row);
                    });
                    table.appendChild(tbody);

                    dataDiv.appendChild(table);

                    var title = document.createElement('h2');
                    title.textContent = cityData.lineName + '钢铁价格趋势表';
                    dataDiv.insertBefore(title, table);
                } else {
                    dataDiv.innerHTML = '<div class="error">未找到钢铁价格数据</div>';
                }
                console.log(res);
            },
            error: function () {
                dataDiv.innerHTML = '<div class="error">加载数据失败，请重试</div>';
            },


        });


    });
})


